﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.Share.Utils
{
    /// <summary>
    /// 文件操作帮助类
    /// </summary>
    public static class FileHelper
    {
        /// <summary>
        /// 拷贝一个文件到另一个文件
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="targetFile"></param>
        public static void CopyFileToFile(string sourceFile, string targetFile)
        {
            if (sourceFile.ToLower() != targetFile.ToLower())
            {
                File.Copy(sourceFile, targetFile, true);
            }
        }

        public static void CopyFileToFolder(string sourceFile, string targetFolder)
        {
            var filename = Path.GetFileName(sourceFile);
            var targetfile = Path.Combine(targetFolder, filename);

            CopyFileToFile(sourceFile, targetfile);
        }

        /// <summary>
        /// 文件重命名
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="targetFile"></param>
        public static void RenameFile(string sourceFile, string targetFile)
        {
            var directoryName = Path.GetDirectoryName(sourceFile);
            var fullName = Path.Combine(directoryName, targetFile);
            File.Move(sourceFile, fullName);
        }

        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public static bool CheckFileExisting(string fullName)
        {
            return File.Exists(fullName);
        }

        /// <summary>
        /// 检查文件夹是否存在
        /// </summary>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public static bool CheckFolderExisting(string fullName)
        {
            return Directory.Exists(fullName);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fullName"></param>
        public static void DeleteFile(string fullName)
        {
            if (File.Exists(fullName))
            {
                File.Delete(fullName);
            }
        }

        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="path"></param>
        public static void DeleteFolder(string path)
        {
            if (Directory.Exists(path))
            {
                ClearAttributes(path);
                Directory.Delete(path, true);
            }
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="path"></param>
        public static void CreateFolder(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// 拷贝一个文件夹到另一个文件夹
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="targetPath"></param>
        public static void CopyFolderToFolder(string sourcePath, string targetPath)
        {
            string[] directories = System.IO.Directory.GetDirectories(sourcePath);
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(sourcePath);
            System.IO.DirectoryInfo[] dirs = dir.GetDirectories();
            copyFile(dir, targetPath);
            if (dirs.Length > 0)
            {
                foreach (System.IO.DirectoryInfo temDirectoryInfo in dirs)
                {
                    string sourceDirectoryFullName = temDirectoryInfo.FullName;
                    string targetDirectoryFullName = sourceDirectoryFullName.Replace(sourcePath, targetPath);
                    if (!System.IO.Directory.Exists(targetDirectoryFullName))
                    {
                        System.IO.Directory.CreateDirectory(targetDirectoryFullName);
                    }
                    copyFile(temDirectoryInfo, targetDirectoryFullName);
                    CopyFolderToFolder(sourceDirectoryFullName, targetDirectoryFullName);
                }
            }
        }

        private static void copyFile(System.IO.DirectoryInfo path, string targetPath)
        {
            string sourcePath = path.FullName;
            System.IO.FileInfo[] files = path.GetFiles();
            foreach (System.IO.FileInfo file in files)
            {
                string sourceFileFullName = file.FullName;
                string destFileFullName = sourceFileFullName.Replace(sourcePath, targetPath);
                file.CopyTo(destFileFullName, true);
            }
        }

        /// <summary>
        /// 获取文件夹中所有文件名
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string[] GetAllFilesFromFolder(string path)
        {
            return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
        }

        public static void ClearAttributes(string path)
        {
            if (Directory.Exists(path))
            {
                string[] subDirs = Directory.GetDirectories(path);
                foreach (string dir in subDirs)
                    ClearAttributes(dir);
                string[] files = files = Directory.GetFiles(path);
                foreach (string file in files)
                    File.SetAttributes(file, FileAttributes.Normal);
            }
        }
    }
}
